<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程id" prop="ProcessInstanceId">
        <el-input
            v-model="queryParams.ProcessInstanceId"
            placeholder="请输入流程实例id"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-select v-model="queryParams.department" placeholder="请选择部门" clearable>
          <el-option
              v-for="dict in dict.type.dept_abbreviation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="改善类型" prop="improvement">
        <el-select v-model="queryParams.improvement" placeholder="请选择改善类型" clearable>
          <el-option
              v-for="dict in dict.type.cont_improve_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择项目状态" clearable>
          <el-option
              v-for="dict in dict.type.cont_improve_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="等级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择等级" clearable>
          <el-option
              v-for="dict in dict.type.cont_improve_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
            v-model="daterangeDate"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="进度" prop="schedule">
        <el-input
            v-model="queryParams.schedule"
            placeholder="请输入进度"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['quality:continuousImprovement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['quality:continuousImprovement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['quality:continuousImprovement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['quality:continuousImprovement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="continuousImprovementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="true" width="96"/>
      <el-table-column label="部门" align="center" prop="department">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dept_abbreviation" :value="scope.row.department"/>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="title" />
      <el-table-column label="改善类型" align="center" prop="improvement">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cont_improve_type" :value="scope.row.improvement"/>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cont_improve_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="推荐等级" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cont_improve_level" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="总分" align="center" prop="totalScore" width="100"/>
      <el-table-column label="均分" align="center" prop="sharing" width="100"/>
      <el-table-column label="提交人" align="center" prop="userName" />
      <el-table-column label="日期" align="center" prop="date">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="schedule" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['quality:continuousImprovement:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['quality:continuousImprovement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改持续改善对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="75%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流程实例id" prop="ProcessInstanceId">
          <el-input v-model="form.ProcessInstanceId" placeholder="请输入流程实例id" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="form.department" placeholder="请选择部门">
            <el-option
                v-for="dict in dict.type.dept_abbreviation"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="改善类型" prop="improvement">
          <el-select v-model="form.improvement" placeholder="请选择改善类型">
            <el-option
                v-for="dict in dict.type.cont_improve_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核类型" prop="audit">
          <el-select v-model="form.audit" placeholder="请选择审核类型">
            <el-option
                v-for="dict in dict.type.cont_improve_audit"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="改善建议" prop="suggestion">
          <el-input v-model="form.suggestions" placeholder="请输入改善建议" />
        </el-form-item>
        <el-form-item label="实施效果" prop="effect">
          <el-input v-model="form.effect" placeholder="请输入实施效果" />
        </el-form-item>
        <el-form-item label="项目状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择项目状态">
            <el-option
                v-for="dict in dict.type.cont_improve_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与人" prop="person">
          <el-input v-model="form.person" placeholder="请输入参与人" />
        </el-form-item>
        <el-form-item label="附件" prop="attachment">
          <el-input v-model="form.attach" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="等级" prop="level">
          <el-select v-model="form.level" placeholder="请选择等级">
            <el-option
                v-for="dict in dict.type.cont_improve_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总分" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入总分" />
        </el-form-item>
        <el-form-item label="评审次数" prop="review">
          <el-input v-model="form.review" placeholder="请输入评审次数" />
        </el-form-item>
        <el-form-item label="均分" prop="sharing">
          <el-input v-model="form.sharing" placeholder="请输入均分" />
        </el-form-item>
        <el-form-item label="解决部门" prop="allDepartment">
          <el-input v-model="form.allDepartment" placeholder="请输入解决部门" />
        </el-form-item>
        <el-form-item label="解决附件" prop="resolveAttachments">
          <image-upload v-model="form.resolveAttachments"/>
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
                          v-model="form.date"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="进度" prop="schedule">
          <el-input v-model="form.schedule" placeholder="请输入进度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContinuousImprovement, getContinuousImprovement, delContinuousImprovement, addContinuousImprovement, updateContinuousImprovement } from "@/api/quality/continuousImprovement";
import { required } from '@/utils/rules'
import { detailProcess } from '@/api/workflow/process'

export default {
  name: "ContinuousImprovement",
  dicts: ['cont_improve_type', 'cont_improve_audit', 'cont_improve_level', 'dept_abbreviation', 'cont_improve_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 持续改善表格数据
      continuousImprovementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分管领导id时间范围
      daterangeDate: [],
      processFormList:[],
      fileList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ProcessInstanceId: undefined,
        department: undefined,
        title: undefined,
        improvement: undefined,
        audit: undefined,
        description: undefined,
        suggestions: undefined,
        effect: undefined,
        status: undefined,
        person: undefined,
        attach: undefined,
        level: undefined,
        totalScore: undefined,
        review: undefined,
        sharing: undefined,
        allDepartment: undefined,
        resolveAttachments: undefined,
        date: undefined,
        schedule: undefined,
        userId: undefined,
        groupId: undefined,
        managerId: undefined,
        leaderId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [required],
        ProcessInstanceId: [required],
        department: [required],
        title: [required],
        improvement: [required],
        description: [required],
        suggestions: [required],
        effect: [required],
        level: [required],
        date: [required],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询持续改善列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null !== this.daterangeDate && '' !== this.daterangeDate) {
        this.queryParams.params["beginDate"] = this.daterangeDate[0];
        this.queryParams.params["endDate"] = this.daterangeDate[1];
      }
      listContinuousImprovement(this.queryParams).then(response => {
        this.continuousImprovementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加持续改善";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getContinuousImprovement(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改持续改善";
      });
      const params = { procInsId: row.processInstanceId }
      detailProcess(params).then(res => {
        const processFormList = res.data.processFormList;
        this.fileList = processFormList[0].fields[9].__config__.defaultValue;
        this.removeUrlString(this.fileList)
        this.form.resolveAttachments = this.fileList;
      })
    },
    removeUrlString(fileList) {
      for (let i = 0; i < fileList.length; i++) {
        const url = fileList[i].url;
        const modifiedUrl = url.replace('https://jierflowable.vip.cpolar.cn', '');
        fileList[i].url = modifiedUrl;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateContinuousImprovement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addContinuousImprovement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除持续改善编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delContinuousImprovement(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('quality/continuousImprovement/export', {
        ...this.queryParams
      }, `持续改善导入表_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
